<?php

namespace App\Http\Middleware;

use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Support\Facades\Hash;
use Validator;
use Illuminate\Support\Facades\Input;
use Closure;

class CheckPermission
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::user()->roles_id!=1){
            $rel = User::getPermission(Auth::user()->roles_id);
            $url = preg_replace("/^(http:).+?(lara4.com)/", "http://lara4.com", url()->current(),1);
            $arr = explode('/',mb_substr($url,17));
            $str =$arr[0].'/'.$arr[1].'/'.$arr[2];
            if ( !in_array($str,$rel) ){
                return redirect('index');
            }
        }
        return $next($request);
    }
}

//wherein
